/******************************************************************
 * Created by: DO HOANG HAI                                       
 * Date of Creation: 25/Dec/2007
 * Date of last modification: 8/Jan/2008
 *                                                                
 * Description: forming Bibtex from components such as author, title...
 * 
 * Status: Optimized and Finished
 ******************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Pbdms.Common
{
    /// <summary>
    /// Object of this class is able to consume a list of bibtex specification
    /// and form a standardized structure of a bibliography reference
    /// </summary>
    class BibTexConstructor
    {
        ArrayList arlComponentStructs;

        public BibTexConstructor(ArrayList arlComponentStructs)
        {
            this.arlComponentStructs = arlComponentStructs;
        }
        /// <summary>
        /// form the bibtex for this arraylist of component bibTex struct
        /// </summary>
        /// <returns></returns>
        public String getBibTex()
        {
            String resultingBibTex = "";
                
            //code here for constructing bibTex
            //layout of the bibtex:
            //author; title; pages; editors; publisher; date of publishing; edition, orgazination; volume
            
            string title = "";
            string author = "";
            string publisher = "";
            string year = "";
            string editor = "";
            string edition = "";
            string volumn = "";
            string pages = "";
            string organization = "";
            
            
            foreach (object obj in arlComponentStructs)
            {
                Common.DataObject.BibTexStruct bts = (Common.DataObject.BibTexStruct)obj;                
                if (!bts.value.Equals(""))
                {
                    switch (bts.name.ToLower())
                    {
                        case "title": title = bts.value; break;
                        case "author": author = bts.value; break;
                        case "publisher": publisher = bts.value; break;
                        case "editor": editor = bts.value; break;
                        case "edition": edition = bts.value; break;
                        case "volume": volumn = bts.value; break;
                        case "pages": pages = bts.value; break;
                        case "organization": organization = bts.value; break;
                        case "year": year = bts.value; break;
                    }                    
                }
            }
            //construct the bibtex with those information using the bibTex format:
            resultingBibTex += author;
            resultingBibTex += ", \"" + title + "\"";
            if (volumn.Length >0)
            {
                resultingBibTex += ", Vol."+ volumn;
            }
            
            if (edition.Length >0)
            {
                switch (edition.Substring(edition.Length -1,1))
                {
                    case "1": edition +="st" ; break;
                    case "2": edition +="nd"; break;
                    case "3": edition +="rd"; break;
                    default: edition += "th"; break;
                }
                edition += " ed.";
                resultingBibTex +=", " + edition;
            }
            if (editor.Length > 0)
                resultingBibTex += ", " + editor;
            if (organization.Length > 0)
                resultingBibTex += ", " + organization;

            if (publisher.Length > 0)
                resultingBibTex += ", " + publisher;
            if (year.Length > 0)
                resultingBibTex += ", " + year;
            if (pages.Length > 0)
                resultingBibTex = resultingBibTex + " (" + pages + ")";

            resultingBibTex += ".";

            return resultingBibTex;

        }
    }
}
